"use strict"

const moment = require('moment')

module.exports = app => {
    const { STRING, DATE, INTEGER } = app.Sequelize
    const AttributeKey = app.model.define('attribute_key', {
        id: {
            type: INTEGER,
            autoIncrement: true,
            primaryKey: true,
        },
        goods_id: {
            type: INTEGER,
            allowNull: false
        },
        name: {
            type: STRING,
            allowNull: false
        },       
        create_time: {
            type: DATE,
            allowNull: false,
            get() {
                return moment(this.getDataValue('create_time')).format('YYYY-MM-DD')
            }
        },
    }) 
    AttributeKey.associate = function() {
        // 关联属性值
        AttributeKey.hasMany(app.model.AttributeValue, { as: 'children', foreignKey: 'attribute_id', targetKey: 'id' });
        
    };
    return AttributeKey
}